<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/main.php';

/**
 * Контроллер карты сайта
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Map extends Main {
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Map
    */
   function Map() {
      parent::Main();
   } // end Map
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      $results = array();
      
      // Получаем список меню
      $this->load->model('m_table');
      $this->m_table->set_table('menus');
      $this->m_table->add_restriction('status', 'active'); 
      $this->m_table->add_restriction('name', 'news', '<>'); 
      $this->m_table->add_sort('position');
      $this->m_table->load_results();
      $menus = $this->m_table->get_results();
      
      // Получаем список страниц для карты сайта
      $this->load->helper('tree');
      $this->m_table->set_table('contents');
      foreach ($menus as $result) {
         $this->m_table->clean();
         $this->m_table->add_restriction('id_menu', $result['id_menu']);
         //$this->m_table->add_restriction('id_parent', null);
         $this->m_table->add_restriction('status', 'active'); 
         $this->m_table->add_sort('position');
         $this->m_table->load_results();
         $contents = $this->m_table->get_results();
         $tree = array_tree($contents, 'id_content', 'id_parent');
         $contents = collapse_tree($tree, 'level');
         $results = array_merge($results, $contents);
      }
      $items = array();
      $this->load->helper('content');
      foreach ($results as $result) {
         $item = array(
            'title' => $result['title'],
            'link'  => get_url($result),
            'level' => (string) (int) 20 * $result['level']
         );
         array_push($items, $item);
      }
      
      // path
      $links = array(
         array(
            'title' => $this->lang->line('front_title'),
            'link'  => base_url()
         ),
         array(
            'title' => $this->lang->line('map_title'),
            'link'  => ''
         )
      );
      $this->load->helper('path');
      $this->add_param('path', create_path($links));
      $this->add_param('link', create_link($this->get_controller(), $this->get_method()));
      
      $this->add_param('title', $this->lang->line('map_title'));
            
      $data = array(
         'items' => $items
      );
      $this->set_content($this->parser->parse('map/body.html', $data, true));
      $this->display();
   } // end index
   
}
